<?php


echo 'Lets make Thumbnails '.'<br/>' ;
echo 'We are going to process these images:<br/>';
// read this directory - same as uploads path - this assumes you only have .jpg files
$path ="/clients/sandbox.guggenheim.org/docs/images/content/arts_curriculum/masters";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
    if($file != "." && $file != "..") {
        echo $path.'/'.$file .'<br/>';
        $images_array[]=$file;
        $i++;
    }
}

thumbMaker($images_array);
continue;
closedir($dh);

		/* function to make thumbnails using
		* @ array
	 	*  returns mixed
		**/

		function thumbMaker($images){
		
		foreach ($images as $image_to_make) {
		
		
		echo 'The Image to make is: '. $image_to_make .'<br/>';
		//global $mainframe;
		
		//variables for size and quality adjust accordingly
		$h_targ_w = 120;
		$v_targ_h = 120;
		$jpeg_quality = 100;
		
		// where the masters live - same as what was read in above. adjust accordinlgy
		$uploads_path = "/clients/sandbox.guggenheim.org/docs/images/content/arts_curriculum/masters" ;
		// where the files will be placed
		$thumb_path = "/clients/sandbox.guggenheim.org/docs/images/content/arts_curriculum/thumbs2" ;
		
		//get some info from image
		$image_stats = GetImageSize($uploads_path.'/'.$image_to_make);
		$imagewidth = $image_stats[0];
		$imageheight = $image_stats[1];
		
		// checks
		echo 'its found here: '.$uploads_path.'/'.$image_to_make.'<br/>';
		echo 'its put here: '.$thumb_path.'/'.$image_to_make.'<br/>';
		echo 'widith is: ' .$imagewidth .'<br/>'; 
		echo 'height is: '. $imageheight .'<br/>';
		if ($imagewidth < $imageheight){
		echo 'true: image is vertical in orientation<br/>';
		} else {
		echo 'false: image is horizontal in orientation<br/>';
		}
		
		// conditions for orientation - horizontal
		if ($imagewidth > $imageheight) {
		
			//do math to determine height
			$ratio = ($imagewidth / $h_targ_w);
			$new_h = round($imageheight / $ratio);
			
			echo 'horizontal ratio is: '.$ratio.'<br/>';
			echo 'new width is :'.$h_targ_w.'<br/>';
			echo 'new height is :'.$new_h.'<br/>';
			
			$src_img = imagecreatefromjpeg($uploads_path."/".$image_to_make);
			$dst_img = imagecreatetruecolor($h_targ_w,$new_h);
			
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$h_targ_w,$new_h,imagesx($src_img),imagesy($src_img));
			imagejpeg($dst_img, $thumb_path."/".$image_to_make, $jpeg_quality);						
			imagedestroy($dst_img);
			
			echo 'thumbmaker made a horizontal image !<br/><br/><hr>' ;
			}
		
		// conditions for orientation - vertical
		if ($imageheight > $imagewidth) {
		
			//do math to determine width
			$ratio = ($imageheight / $v_targ_h);
			$new_w = round($imagewidth / $ratio);
			
			echo 'vertical ratio is: '.$ratio.'<br/>';
			echo 'new width is :'.$new_w.'<br/>';
			echo 'new height is :'.$v_targ_h.'<br/>';
			
			$src_img = imagecreatefromjpeg($uploads_path."/".$image_to_make);
			$dst_img = imagecreatetruecolor($new_w,$v_targ_h);
		
			imagecopyresampled($dst_img,$src_img,0,0,0,0,$new_w,$v_targ_h,imagesx($src_img),imagesy($src_img));
			imagejpeg($dst_img, $thumb_path."/".$image_to_make, $jpeg_quality);
			imagedestroy($dst_img);
			
			echo 'thumbmaker made a vertical image !<br/><br/><hr>' ;
			}
		
		
		} //end of loop
		echo ' All Thumbnails made by thumbmaker !<br/>' ;
		exit;
		
	}


?>